﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample122;

public class BallData
{
    public float[] Data { get; set; }
    public int VCount { get; set; }
    public int ByteCount { get; set; }

    public void GenBallData(float angleSpan)
    {
        const float r = 1.0f;
        List<float> alVertix = new List<float>();
        List<float> alLongLat = new List<float>();
        for (float vAngle = -90; vAngle < 90; vAngle = vAngle + angleSpan)
        {
            for (float hAngle = 0; hAngle < 360; hAngle = hAngle + angleSpan)
            {
                float x0 = (float)(r * MathF.Cos(toRadians(vAngle)) * MathF.Cos(toRadians(hAngle)));
                float y0 = (float)(r * MathF.Cos(toRadians(vAngle)) * MathF.Sin(toRadians(hAngle)));
                float z0 = (float)(r * MathF.Sin(toRadians(vAngle)));
                float long0 = hAngle; float lat0 = vAngle;
                float x1 = (float)(r * MathF.Cos(toRadians(vAngle)) * MathF.Cos(toRadians(hAngle + angleSpan)));
                float y1 = (float)(r * MathF.Cos(toRadians(vAngle)) * MathF.Sin(toRadians(hAngle + angleSpan)));
                float z1 = (float)(r * MathF.Sin(toRadians(vAngle)));
                float long1 = hAngle + angleSpan; float lat1 = vAngle;
                float x2 = (float)(r * MathF.Cos(toRadians(vAngle + angleSpan)) * MathF.Cos(toRadians(hAngle + angleSpan)));
                float y2 = (float)(r * MathF.Cos(toRadians(vAngle + angleSpan)) * MathF.Sin(toRadians(hAngle + angleSpan)));
                float z2 = (float)(r * MathF.Sin(toRadians(vAngle + angleSpan)));
                float long2 = hAngle + angleSpan; float lat2 = vAngle + angleSpan;
                float x3 = (float)(r * MathF.Cos(toRadians(vAngle + angleSpan)) * MathF.Cos(toRadians(hAngle)));
                float y3 = (float)(r * MathF.Cos(toRadians(vAngle + angleSpan)) * MathF.Sin(toRadians(hAngle)));
                float z3 = (float)(r * MathF.Sin(toRadians(vAngle + angleSpan)));
                float long3 = hAngle; float lat3 = vAngle + angleSpan;
                alVertix.Add(x1); alVertix.Add(y1); alVertix.Add(z1);
                alVertix.Add(x3); alVertix.Add(y3); alVertix.Add(z3);
                alVertix.Add(x0); alVertix.Add(y0); alVertix.Add(z0);
                alVertix.Add(x1); alVertix.Add(y1); alVertix.Add(z1);
                alVertix.Add(x2); alVertix.Add(y2); alVertix.Add(z2);
                alVertix.Add(x3); alVertix.Add(y3); alVertix.Add(z3);
                alLongLat.Add(long1); alLongLat.Add(lat1);
                alLongLat.Add(long3); alLongLat.Add(lat3);
                alLongLat.Add(long0); alLongLat.Add(lat0);
                alLongLat.Add(long1); alLongLat.Add(lat1);
                alLongLat.Add(long2); alLongLat.Add(lat2);
                alLongLat.Add(long3); alLongLat.Add(lat3);
            }
        }
        VCount = alVertix.Count / 3;
        ByteCount = VCount * 8 * sizeof(float);
        Data = new float[VCount * 8];
        int index = 0;
        for (int i = 0; i < VCount; i++)
        {
            Data[index++] = alVertix[i * 3 + 0];
            Data[index++] = alVertix[i * 3 + 1];
            Data[index++] = alVertix[i * 3 + 2];

            Data[index++] = alVertix[i * 3 + 0] / r;
            Data[index++] = alVertix[i * 3 + 1] / r;
            Data[index++] = alVertix[i * 3 + 2] / r;

            Data[index++] = alLongLat[i * 2 + 0];
            Data[index++] = alLongLat[i * 2 + 1];
        }
    }

    float toRadians(float degree)
    {
        return (float)(degree * Math.PI / 180.0);
    }
}
